package watchTracker;

import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;


public class dbManager {
	private Connection connect = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;
	private tvShowData[] data = new tvShowData[100];
	private core coreRef;
	
	public dbManager(core coreRef){
		this.coreRef = coreRef;
		try{
			// This will load the MySQL driver, each DB has its own driver
			Class.forName("com.mysql.jdbc.Driver");
			
			// Setup the connection with the DB
			connect = DriverManager.getConnection("jdbc:mysql://10.10.10.1/tvshows?"
							+ "user=root&password=10810");

			// Statements allow to issue SQL queries to the database
			statement = connect.createStatement();
			
			
			
		}catch (Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	public void readDataBase() {
		try {
			// tvshows results set and send to gui
			resultSet = statement.executeQuery("select * from tvshows");
			coreRef.addShowNameGUI("");
			while(resultSet.next()){
				coreRef.addShowNameGUI(resultSet.getString("name"));
			}
			
			// genre results and send to gui
			resultSet = statement.executeQuery("select * from genre");
			while(resultSet.next()){
				coreRef.addGenreGUI(resultSet.getString("genre"));
			}			
		} catch (SQLException e) {
			System.out.println(e.getSQLState());
			System.out.println(e.getMessage());
			System.out.println(e.getCause());
		}
	}
	
	//add functions
	public void addGenre(String genre){
		System.out.println(genre);
		try{
				statement.execute("INSERT INTO genre (genre) values('"+genre+"')");
		}catch(SQLException e){
			System.out.println(e.getSQLState());
			System.out.println(e.getMessage());
			System.out.println(e.getCause());
		}
	}
	
	public void addShow(String name, String genre){
		try{
			statement.execute("INSERT INTO tvshows (name,genre) values('"+name+"','"+genre+"')");
		}catch(SQLException e){
			System.out.println(e.getSQLState());
			System.out.println(e.getMessage());
			System.out.println(e.getCause());
		}
	}
	public void addEpisode(String showname, String title, int seasonNumb, int episodeNumb, int quality, int rating, String summary, String fileName){
		try{
			statement.execute("INSERT INTO " +
					"episodes (showname,episodetitle,seasonnumber,episodenumber, quality, rating, summary) " +
					"values('"+showname+"','"+title+"',"+seasonNumb+","+episodeNumb+","+quality+","+rating+",'"+summary+"')");			
		}catch(SQLException e){
			System.out.println(e.getSQLState());
			System.out.println(e.getMessage());
			System.out.println(e.getCause());
		}
	}

	
	// You need to close the resultSet
	private void close() {
		try {
			if (resultSet != null) {
				resultSet.close();
			}

			if (statement != null) {
				statement.close();
			}

			if (connect != null) {
				connect.close();
			}
		} catch (Exception e) {

		}
	}

}
